<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="formContainer">
        <el-button class="buttons" icon="el-icon-add" @click="$router.push({name:'pagesAdd'})">
          添加
        </el-button>
      </div>
      <div class="formContainer">
        <label class="filterLabel">关键词 :</label>
        <el-input v-model="listQuery.keyword" placeholder="请输入关键词" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <label class="filterLabel">是否显示:</label>
        <el-select v-model="listQuery.isshow" class="search-select" clearable placeholder="请选择">
          <el-option label="显示" :value="1" />
          <el-option label="隐藏" :value="2" />
        </el-select>
        <el-button class="buttons" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
      </div>
    </div>
    <el-table :key="tableKey" :data="list" style="width: 100%;" :header-cell-style="{background:'#ebebeb',height:'#54px'}" @sort-change="sortChange">
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="代码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.service_agreement }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="发布时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.addtime }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="展示" align="center">
        <template slot-scope="{row}">
          <span>{{ row.isshow_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="排序" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button
            type="primary"
            size="mini"
            @click="$router.push({name:'pagesAdd',query: {
              id: row.id
            }})"
          >
            编辑
          </el-button>
          <el-popconfirm
            v-if="false"
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="删除后不可以恢复，确定删除吗？"
            @onConfirm="handleDelete(row,$index)"
          >
            <el-button slot="reference" size="mini" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.per_page" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, del } from '@/api/pages'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { notifySuccess } from '@/utils/message'
export default {
  components: { Pagination },
  data() {
    return {
      iconsShow: false,
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        per_page: 15,
        importance: undefined,
        keyword: null,
        type: undefined,
        sort: '+id',
        addtimestart: null,
        addtimeend: null,
        isshow: null,
        ishot: null,
        istop: null,
        ac_id: null
      },
      dataRange: [],
      importanceOptions: [1, 2, 3],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: { 'normal': '正常', 'hidden': '隐藏' },
      downloadLoading: false,
      groupOption: {},
      cateList: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      var that = this
      this.listLoading = true
      if (this.dataRange.length === 2) {
        this.listQuery.addtimestart = this.dataRange[0] / 1000
        this.listQuery.addtimeend = this.dataRange[1] / 1000
      }
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          that.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    // 删除操作
    handleDelete(row, index) {
      var that = this
      del(row.id).then(res => {
        notifySuccess(res.msg || '删除成功')
        that.list.splice(index, 1)
      })
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
<style scoped>
.cates-select{
   width: 120px;
}
.search-select{
  width: 100px;
}

</style>
